﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net.Http;
using System.Net.Http.Headers;

namespace System.Net.Http.Integration.Tests
{
    [TestClass]
    public class IntegrationTests
    {
        /// <summary>
        /// Test if proxy actually forwards GetAsync()
        /// </summary>
        [TestMethod] public void CanAccessService()
        {
            var uri = new Uri(@"https://api.bitbucket.org/2.0/users/raptux/");

            IHttpClient proxy = new HttpClientProxy();
            var asyncResult = proxy.GetAsync(uri);

            var result = asyncResult.Result;
            var content = result.Content.ReadAsAsync<dynamic>().Result;
            
            Assert.IsTrue(result.IsSuccessStatusCode);
            Assert.AreEqual(content["username"].Value, "raptux");
            Assert.AreEqual(content["type"].Value, "user");
        }

        [TestMethod] public void CanSetBaseAddress()
        {
            var uri = new Uri(@"https://api.bitbucket.org/2.0/users/raptux");

            IHttpClient proxy = new HttpClientProxy();
            proxy.DefaultRequestHeaders
                .Accept
                .Add(new MediaTypeWithQualityHeaderValue("text/integration-test"));

            var result = proxy.GetAsync(uri).Result;

            Assert.IsTrue(result.RequestMessage.Headers.Accept.Contains(new MediaTypeWithQualityHeaderValue("text/integration-test")));
        }
    }
}
